package com.eascs.common.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

public class ObjectArrayUtil {
	/**
	 * 根据指定数据位置重新组合数组
	 * 
	 * @param objsList
	 * @param itemIndexs
	 * @return
	 */
	public static List<Object[]> buildObjectArrayList(List<Object[]> objsList, int[] itemIndexs) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (Object[] objects : objsList) {
			Object[] objs = new Object[itemIndexs.length];
			for (int i = 0; i < objs.length; i++) {
				objs[i] = objects[itemIndexs[i]];
			}
			list.add(objs);
		}
		return list;
	}

	/**
	 * 根据指定对象属性重新组合数组
	 * 
	 * @param objList
	 * @param propertyNames
	 * @return
	 */
	public static List<Object[]> buildObjectArrayList(List<Object> objList, String[] propertyNames) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (Object obj : objList) {
			Object[] objs = new Object[propertyNames.length];
			for (int i = 0; i < objs.length; i++) {
				try {
					objs[i] = PropertyUtils.getProperty(obj, propertyNames[i]);
				} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
					throw new RuntimeException(e);
				}
			}
			list.add(objs);
		}
		return list;
	}
}
